package com.kifas.service.dataaccess;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kifas.entity.JenisBarangEntity;

@Repository
public class JenisBarangDao {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(JenisBarangDao.class);

	private final static String ALL_JENIS_BARANG = "from JenisBarangEntity jb ";
	private final static String JENIS_BARANG_BY_ID = ALL_JENIS_BARANG
			+ "where jb.idJenisBarang = :idJenisBarang";

	@Autowired
	private SessionFactory sessionFactory;

	public void insert(JenisBarangEntity jenisBarang) throws Exception {
		sessionFactory.getCurrentSession().save(jenisBarang);
	}

	@SuppressWarnings("unchecked")
	public List<JenisBarangEntity> getJenisBarangs() {
		return sessionFactory.getCurrentSession().createQuery(ALL_JENIS_BARANG)
				.list();
	}

	public JenisBarangEntity getJenisBarang(Long idJenisBarang) {
		return (JenisBarangEntity) sessionFactory.getCurrentSession()
				.createQuery(JENIS_BARANG_BY_ID)
				.setLong("idJenisBarang", idJenisBarang).uniqueResult();
	}
	
	public void update(JenisBarangEntity jenisBarang) throws Exception {
		sessionFactory.getCurrentSession().update(jenisBarang);
	}
}
